// wii libraries
#include <grrlib.h>

// custom libraries
#include "..\..\include\logger.h"
#include "..\..\include\graphics\image.h"
#include "..\..\include\graphics\graphics_renderer.h" // matching header


// ==================================
//    CONSTRUCTORS and DESTRCUTORS
// ==================================

GraphicsRenderer::GraphicsRenderer()
{
	initializeLibrary();
}

GraphicsRenderer::~GraphicsRenderer()
{
	exitLibrary();
}


// ==================================
//          DRAW FUNCTIONS
// ==================================

void GraphicsRenderer::drawImage(Image& image, int x, int y)
{
	GRRLIB_texImg* tex = (GRRLIB_texImg*)image.getData();
	GRRLIB_DrawImg(x, y, tex, 0, 1, 1, 0xFFFFFFFF);
}

void GraphicsRenderer::printText(std::string line, int x, int y,
	Font& font, unsigned int fontSize, unsigned int RGBA)
{
	GRRLIB_ttfFont* fnt = (GRRLIB_ttfFont*)font.getData();
	GRRLIB_PrintfTTF(x, y, fnt, line.c_str(), fontSize, (u32)RGBA);
}

void GraphicsRenderer::fillScreen(unsigned int RGBA)
{
	GRRLIB_FillScreen((u32)RGBA);
}

void GraphicsRenderer::render()
{
	GRRLIB_Render();
}


// ==================================
//       INITIALIZE and EXIT
// ==================================

bool GraphicsRenderer::initializeLibrary()
{
	Logger* log = Logger::Instance();
	log->addEntry(Logger::INFO, 
		"GraphicsRenderer: Initializing GRRLIB...");

	bool success = false;

	switch (GRRLIB_Init()) {
		case 0 :
			log->addEntry(Logger::INFO, 
				"GraphicsRenderer: Initialization successful.");
			success = true;
			break;
		case -1 :
			log->addEntry(Logger::ERROR, 
				"GraphicsRenderer: Not enough memory.");
			success = false;
			break;
		case -2 :
			log->addEntry(Logger::ERROR, 
				"GraphicsRenderer: No filling system.");
			success = false;
			break;
		case -3 :
			log->addEntry(Logger::ERROR,
				"GraphicsRenderer: Cannot initialize font engine.");
			success = false;
			break;
	}		
	
	return success;
}

bool GraphicsRenderer::exitLibrary()
{
	Logger::Instance()->addEntry(Logger::INFO, 
		"GraphicsRenderer: Exiting GRRLIB...");
	
	GRRLIB_Exit();
	
	Logger::Instance()->addEntry(Logger::INFO, 
		"GraphicsRenderer: Exit successful.");
	return true;
}
